"""生成占位资源：hit.wav、mole.png、hole.png

该脚本使用标准库生成简单的 WAV（正弦包络）。
使用 pygame 绘制并保存 PNG 占位图（需安装 pygame）。
"""
import os
import math
import wave
import struct
import pygame

BASE = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ASSETS_DIR = os.path.join(BASE, 'assets')
os.makedirs(ASSETS_DIR, exist_ok=True)

# 生成简单的短促 hit.wav（440Hz 正弦包络）
def generate_hit_wav(path, duration_s=0.18, freq=880.0, volume=0.5, samplerate=44100):
    n_samples = int(samplerate * duration_s)
    with wave.open(path, 'w') as wf:
        wf.setnchannels(1)
        wf.setsampwidth(2)  # 16-bit
        wf.setframerate(samplerate)
        for i in range(n_samples):
            t = i / samplerate
            # 包络：快速上升、指数衰减
            env = (1 - math.exp(-20 * t)) * math.exp(-6 * t)
            sample = int(volume * 32767.0 * env * math.sin(2 * math.pi * freq * t))
            wf.writeframes(struct.pack('<h', sample))

# 使用 pygame 绘制 mole 与 hole 占位 PNG
def generate_images(mole_path, hole_path, size=(128, 128)):
    pygame.init()
    surf = pygame.Surface(size, pygame.SRCALPHA)

    # hole: 深色圆环
    surf.fill((0, 0, 0, 0))
    cx, cy = size[0] // 2, size[1] // 2
    pygame.draw.circle(surf, (80, 50, 30), (cx, cy), 54)
    pygame.draw.circle(surf, (30, 20, 10), (cx, cy), 44)
    pygame.image.save(surf, hole_path)

    # mole: 可爱圆形带眼睛
    surf2 = pygame.Surface(size, pygame.SRCALPHA)
    surf2.fill((0, 0, 0, 0))
    pygame.draw.circle(surf2, (170, 120, 60), (cx, cy+6), 44)  # 身体
    pygame.draw.circle(surf2, (200, 160, 110), (cx, cy-8), 30)  # 头部
    # 眼睛
    pygame.draw.circle(surf2, (255, 255, 255), (cx-8, cy-10), 6)
    pygame.draw.circle(surf2, (0, 0, 0), (cx-8, cy-10), 3)
    pygame.draw.circle(surf2, (255, 255, 255), (cx+8, cy-10), 6)
    pygame.draw.circle(surf2, (0, 0, 0), (cx+8, cy-10), 3)
    # 鼻子
    pygame.draw.circle(surf2, (255, 120, 120), (cx, cy-2), 5)
    pygame.image.save(surf2, mole_path)


def main():
    hit_path = os.path.join(ASSETS_DIR, 'hit.wav')
    mole_path = os.path.join(ASSETS_DIR, 'mole.png')
    hole_path = os.path.join(ASSETS_DIR, 'hole.png')

    print('生成:', hit_path, mole_path, hole_path)
    generate_hit_wav(hit_path)
    generate_images(mole_path, hole_path)
    print('生成完成')

if __name__ == '__main__':
    main()
